library(foreign)
library(tidyverse)
library(readstata13)

rm(list = ls())

data1<-read.csv("Code/Study1/Study1_Clean.csv")

data2<-data.frame(data1)
colnames(data2)
summary(data2)

#Step 1 - Coding Conjoint Up####
resp_id <- rownames(data2)
resp_id
data3 <- cbind(resp_id, data2)
colnames(data3)
data3$resp_id

##Code Difference in Intensity Score 

data3<-data3 %>% 
  mutate("1_diff1" = X1_intensity_1-X1_intensity_2) %>% 
  mutate("1_diff2"= X1_intensity_2-X1_intensity_1) %>% 
  mutate("2_diff1" = X2_intensity_1-X2_intensity_2) %>% 
  mutate("2_diff2"= X2_intensity_2-X2_intensity_1) %>% 
  mutate("3_diff1" = X3_intensity_1-X3_intensity_2) %>% 
  mutate("3_diff2"= X3_intensity_2-X3_intensity_1) %>% 
  mutate("4_diff1" = X4_intensity_1-X4_intensity_2) %>% 
  mutate("4_diff2"= X4_intensity_2-X4_intensity_1) %>% 
  mutate("5_diff1" = X5_intensity_1-X5_intensity_2) %>% 
  mutate("5_diff2"= X5_intensity_2-X5_intensity_1) %>% 
  mutate("6_diff1" = X6_intensity_1-X6_intensity_2) %>% 
  mutate("6_diff2"= X6_intensity_2-X6_intensity_1) 
  

##Rename DV
data4<-data3 %>% 
  rename(
    "1_pref" = "X1_pref",
    "2_pref" = "X2_pref",
    "3_pref" = "X3_pref",
    "4_pref" = "X4_pref",
    "5_pref" = "X5_pref",
    "6_pref" = "X6_pref",
    "1_intent1" = "X1_intensity_1",
    "2_intent1" = "X2_intensity_1",
    "3_intent1" = "X3_intensity_1",
    "4_intent1" = "X4_intensity_1",
    "5_intent1" = "X5_intensity_1",
    "6_intent1" = "X6_intensity_1",
    "1_intent2" = "X1_intensity_2",
    "2_intent2" = "X2_intensity_2",
    "3_intent2" = "X3_intensity_2",
    "4_intent2" = "X4_intensity_2",
    "5_intent2" = "X5_intensity_2",
    "6_intent2" = "X6_intensity_2")
colnames(data4)

d_master_conjoint<-data4

#Formatting Dataset so that each polling location has a row. Includes: 
#Experiment values displayed to respondent 
d_loc <- d_master_conjoint %>% select(resp_id, starts_with("choice"))
d_loc <- d_loc %>% 
  gather(variable, value, -resp_id) %>% 
  mutate( 
    choiceNum=gsub("[A-Za-z]|_.+","",variable),
    locNum=gsub(".+(.$)","\\1",variable),
    attribute=gsub(".+_|.$","",variable)
  ) %>%select(-variable) %>% 
  spread(attribute, value)
d_loc
d_loc %>% filter(resp_id==1)

#Reshape - Preference record. This dataset records which polling location (1 or 2) the respondent chose 
d_pref <- d_master_conjoint %>% select(resp_id, ends_with("pref"))
d_pref <- d_pref %>%  
  gather(variable, preference, -resp_id) %>% 
  mutate( 
    choiceNum = gsub("_pref","",variable),
    preference=as.numeric(gsub("","",preference))
  ) %>% 
  select(-variable)
d_pref
d_pref %>% filter(resp_id==1)

#Reshape - Intensity difference. This dataset records the preference difference 
d_diff1 <- d_master_conjoint %>% select(resp_id, ends_with("diff1"))
d_diff1 <- d_diff1 %>%  
  gather(variable, diff, -resp_id) %>% 
  mutate( 
    choiceNum = gsub("_diff1","",variable),
    diff=as.numeric(gsub("","",diff))
  ) %>% 
  select(-variable)

d_diff2 <- d_master_conjoint %>% select(resp_id, ends_with("diff2"))
d_diff2<- d_diff2 %>%  
  gather(variable, diff, -resp_id) %>% 
  mutate( 
    choiceNum = gsub("_diff2","",variable),
    diff=as.numeric(gsub("","",diff))
  ) %>% 
  select(-variable)

d_diff1<-d_diff1 %>% add_column(locNum=as.character(1))
d_diff2<-d_diff2 %>% add_column(locNum=as.character(2))
d_diff1 %>% filter(resp_id==1) 
d_diff2 %>% filter(resp_id==1) 

d_diff_merged<-bind_rows(d_diff1, d_diff2) 
d_diff_merged %>% filter(resp_id==1) 


##Stack Dataset 
#Step 1 - Join the dataset with conjoint attributes with the dataset that records profile preference 
d_stack <- left_join(d_loc, d_pref)

#Step 2 - Join the datasets that measure profile preference intensity 
d_stack2<-left_join(d_stack, d_diff_merged)

d_stack2 %>% filter(resp_id==2)

d_stack3 <- d_stack2 %>% 
  mutate (
    Y_choice=as.numeric(locNum==preference),
  )
d_stack3

d_stack3 %>% filter(resp_id==1)

#Data Check 
nrow(d_stack3) 
summary(d_stack3)

##Merge Back in Individual Level Data
dstack_merge<- left_join(d_stack3, data3, by="resp_id")
summary(dstack_merge)
nrow(dstack_merge)

##Write 
save.dta13(dstack_merge, "Code/Study1/merged_conjoint_study1.dta")

